package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.bean.SystemContextHolder;
import com.base.vistter.iframe.entity.InverterCollect;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.InverterCollectService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/collect")
public class InverterCollectController {
    @Resource(name = "inverterCollectService")
    private InverterCollectService inverterCollectService;


    @GetMapping("/update")
    public Result<Boolean> update(Integer stationId) throws IframeException {
        LambdaQueryWrapper<InverterCollect> lambdaQueryWrapper = new LambdaQueryWrapper<InverterCollect>();
        lambdaQueryWrapper.eq(InverterCollect::getSysUserId, SystemContextHolder.getSessionContext().getUserId()).eq(InverterCollect::getInverterStationId, stationId);
        InverterCollect inverterCollect = inverterCollectService.selectOne(lambdaQueryWrapper);
        if (inverterCollect != null) {
            inverterCollectService.delete(inverterCollect.getId());
        } else {
            inverterCollect = new InverterCollect(stationId, SystemContextHolder.getSessionContext().getUserId());
            inverterCollectService.save(inverterCollect);
        }
        return Result.ok(true);
    }

}
